package com.edu.hre.common.mobile;

import java.io.IOException;
import java.io.Writer;
import java.text.ParseException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.struts.action.dashboard.DashLeaveAction;
import com.edu.hre.struts.action.dashboard.DashRecruitmentAction;
import com.edu.hre.struts.action.dashboard.DashResourceAction;

/**
 * Servlet implementation class MobileAPI
 */
public class MobileAPI extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MobileAPI() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String type = request.getParameter("type");
			String rp = "invalid query string ...";
			if("leave".equalsIgnoreCase(type) || "dashLeave".equalsIgnoreCase(type)){
				rp = this.getAPILeave(request);
			} else if("human".equalsIgnoreCase(type) || "dashResource".equalsIgnoreCase(type)){
				rp = this.getAPIHuman(request);
			} else if("recruitment".equalsIgnoreCase(type) || "dashRecruitment".equalsIgnoreCase(type)){
				rp = this.getAPIRecruitment(request);
			}
			response.setContentType("text");
			String cb = request.getParameter("callback");
			String contentType = request.getParameter("contentType");
			if(StringUtils.isNotBlank(contentType)){
				response.setContentType(contentType);
			}
			Writer out = response.getWriter();
			if (cb != null) {
			    out.write(cb + "(");
			}
			out.write(rp);
			if (cb != null) {
			    out.write(");");
			}
			
//			response.setCharacterEncoding("UTF-8");
//			byte[] dataByte = CommonUtils.convertStringToByteArray(rp, "UTF-8");
//			response.setContentLength(dataByte.length);
//			response.getOutputStream().write(dataByte);
			
		} catch (Exception e) {
			CommonUtils.println(e.getMessage());
		}
	}
	
	private JSONObject getJSON(String key, Object value){
		JSONObject obj = new JSONObject();
		obj.put(key, value);
		return obj;
	}

	private String getAPIRecruitment(HttpServletRequest request) {
		DashRecruitmentAction.executeStatic(request);
		String str = "";
		str += "topic:" + (String) request.getAttribute("pie_topic") + "\n";
		str += "name:" + (String) request.getAttribute("pie_partsName") + "\n";
		str += "number:" + (String) request.getAttribute("pie_partsNumber") + "\n";
		str += "total:" + (Float) request.getAttribute("pie_totalLeaveDays") + "\n";
		str += "=====\n";
		str += "topic:" + (String) request.getAttribute("bar2_topic") + "\n";
		str += "name:" + (String) request.getAttribute("bar2_partsName") + "\n";
		str += "number:" + (String) request.getAttribute("bar2_partsNumber") + "\n";
		CommonUtils.println(str);
		
		JSONObject topic = new JSONObject(); 
		
		if("1".equals(request.getParameter("n"))){
			JSONArray arr1 = new JSONArray();
			String indexName[] = ((String) request.getAttribute("pie_partsName")).split(";");
			String indexNumber[] = ((String) request.getAttribute("pie_partsNumber")).split(";");
			for(int i = 0; i < indexName.length && i < indexNumber.length; i ++){
				if(!"0".equals(indexNumber[i])){
					JSONObject tmp = getJSON("active", indexName[i]);
					tmp.put("number", Float.parseFloat(indexNumber[i]));
					arr1.add(tmp);
				}
			}
			topic.put((String) request.getAttribute("pie_topic"), arr1);
		} else if("2".equals(request.getParameter("n"))){
			JSONArray arr1 = new JSONArray();
			String indexName[] = ((String) request.getAttribute("bar2_partsName")).split(";");
			String indexNumber[] = ((String) request.getAttribute("bar2_partsNumber")).split(";");
			for(int i = 0; i < indexName.length && i < indexNumber.length; i ++){
				JSONObject tmp = getJSON("unit", indexName[i]);
				tmp.put("number", Float.parseFloat(indexNumber[i]));
				arr1.add(tmp);
			}
			topic.put((String) request.getAttribute("bar2_topic"), arr1);
		}  
		
		return topic.toString();
	}

	private String getAPIHuman(HttpServletRequest request) {
		DashResourceAction.executeStatic(request);
		String str = "";
		str += "topic:" + (String) request.getAttribute("pie_topic") + "\n";
		str += "name:" + (String) request.getAttribute("pie_partsName") + "\n";
		str += "number:" + (String) request.getAttribute("pie_partsNumber") + "\n";
		str += "total:" + (Float) request.getAttribute("pie_totalLeaveDays") + "\n";
		str += "=====\n";
		str += "topic:" + (String) request.getAttribute("pie2_topic") + "\n";
		str += "name:" + (String) request.getAttribute("pie2_partsName") + "\n";
		str += "number:" + (String) request.getAttribute("pie2_partsNumber") + "\n";
		str += "total:" + (Float) request.getAttribute("pie2_totalLeaveDays") + "\n";
		str += "=====\n";
		str += "topic:" + (String) request.getAttribute("bar_topic") + "\n";
		str += "name:" + (String) request.getAttribute("bar_partsName") + "\n";
		str += "number:" + (String) request.getAttribute("bar_partsNumber") + "\n";
		CommonUtils.println(str);
		
		JSONObject topic = new JSONObject(); 
		
		if("1".equals(request.getParameter("n"))){
			JSONArray arr1 = new JSONArray();
			String indexName[] = ((String) request.getAttribute("pie_partsName")).split(";");
			String indexNumber[] = ((String) request.getAttribute("pie_partsNumber")).split(";");
			for(int i = 0; i < indexName.length && i < indexNumber.length; i ++){
				if(!"0".equals(indexNumber[i])){
					JSONObject tmp = getJSON("gender", indexName[i]);
					tmp.put("number", Float.parseFloat(indexNumber[i]));
					arr1.add(tmp);
				}
			}
			topic.put((String) request.getAttribute("pie_topic"), arr1);
		} else if("2".equals(request.getParameter("n"))){
			JSONArray arr1 = new JSONArray();
			String indexName[] = ((String) request.getAttribute("pie2_partsName")).split(";");
			String indexNumber[] = ((String) request.getAttribute("pie2_partsNumber")).split(";");
			for(int i = 0; i < indexName.length && i < indexNumber.length; i ++){
				if(!"0".equals(indexNumber[i])){
					JSONObject tmp = getJSON("age", indexName[i]);
					tmp.put("number", Float.parseFloat(indexNumber[i]));
					arr1.add(tmp);
				}
			}
			topic.put((String) request.getAttribute("pie2_topic"), arr1);
		} else if("3".equals(request.getParameter("n"))){
			JSONArray arr1 = new JSONArray();
			String indexName[] = ((String) request.getAttribute("bar_partsName")).split(";");
			String indexNumber[] = ((String) request.getAttribute("bar_partsNumber")).split(";");
			for(int i = 0; i < indexName.length && i < indexNumber.length; i ++){
				JSONObject tmp = getJSON("unit", indexName[i]);
				tmp.put("number", Float.parseFloat(indexNumber[i]));
				arr1.add(tmp);
			}
			topic.put((String) request.getAttribute("bar_topic"), arr1);
		}
		
		return topic.toString();
	} 

	private String getAPILeave(HttpServletRequest request) throws ParseException {
		DashLeaveAction.executeStatic(request, null, null);
		String str = "";
		str += "topic:" + (String) request.getAttribute("pie_topic") + "\n";
		str += "name:" + (String) request.getAttribute("pie_partsName") + "\n";
		str += "number:" + (String) request.getAttribute("pie_partsNumber") + "\n";
		str += "total:" + (Float) request.getAttribute("pie_totalLeaveDays") + "\n";
		str += "=====\n";
		str += "topic:" + (String) request.getAttribute("bar_topic") + "\n";
		str += "number:" + (String) request.getAttribute("bar_partsNumber") + "\n";
		str += "=====\n";
		str += "topic:" + (String) request.getAttribute("bar2_topic") + "\n";
		str += "name:" + (String) request.getAttribute("bar2_partsName") + "\n";
		str += "number:" + (String) request.getAttribute("bar2_partsNumber") + "\n";
		CommonUtils.println(str);
		
		JSONObject topic = new JSONObject(); 
		
		if("1".equals(request.getParameter("n"))){
			JSONArray arr1 = new JSONArray();
			String indexName[] = ((String) request.getAttribute("pie_partsName")).split(";");
			String indexNumber[] = ((String) request.getAttribute("pie_partsNumber")).split(";");
			for(int i = 0; i < indexName.length && i < indexNumber.length; i ++){
				if(!"0".equals(indexNumber[i])){
					JSONObject tmp = getJSON("type", indexName[i]);
					tmp.put("number", Float.parseFloat(indexNumber[i]));
					arr1.add(tmp);
				}
			}
			topic.put((String) request.getAttribute("pie_topic"), arr1);
		} else if("2".equals(request.getParameter("n"))){
			JSONArray arr1 = new JSONArray();
			String indexName[] = ((String) request.getAttribute("bar_partsName")).split(";");
			String indexNumber[] = ((String) request.getAttribute("bar_partsNumber")).split(";");
			for(int i = 0; i < indexName.length && i < indexNumber.length; i ++){
				JSONObject tmp = getJSON("month", indexName[i]);
				tmp.put("number", Float.parseFloat(indexNumber[i]));
				arr1.add(tmp);
			}
			topic.put((String) request.getAttribute("bar_topic"), arr1);
		} else if("3".equals(request.getParameter("n"))){
			JSONArray arr1 = new JSONArray();
			String indexName[] = ((String) request.getAttribute("bar2_partsName")).split(";");
			String indexNumber[] = ((String) request.getAttribute("bar2_partsNumber")).split(";");
			for(int i = 0; i < indexName.length && i < indexNumber.length; i ++){
				JSONObject tmp = getJSON("unit", indexName[i]);
				tmp.put("number", Float.parseFloat(indexNumber[i]));
				arr1.add(tmp);
			}
			topic.put((String) request.getAttribute("bar2_topic"), arr1);
		}
		
		return topic.toString();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
